import {Injectable} from "@angular/core";
import {Subject, Observable} from "rxjs";
import {isUndefined} from "ionic-angular/util/util";
/**
 * Created by jackbranson on 17/3/6.
 */

@Injectable()
export class EditorService {
  private static editorLinkSource = new Map<string, Subject<string>>();

  sendEmoji(emoji_input_id: string, emoji_id: string): void
  {
    EditorService.editorLinkSource.get(emoji_input_id).next(emoji_id);
  }

  getSource(emoji_input_id: string): Observable<string>
  {
    let ret = EditorService.editorLinkSource.get(emoji_input_id);
    if (isUndefined(ret)){
      ret = new Subject<string>();
      EditorService.editorLinkSource.set(emoji_input_id, ret);
    }
    return ret.asObservable();
  }

  registerEmojiInput(emoji_input_id: string): void
  {
    let tmp = EditorService.editorLinkSource.get(emoji_input_id);
    if (isUndefined(tmp)) EditorService.editorLinkSource.set(emoji_input_id, new Subject<string>());
  }
}
